package org.gdstash.character;

import org.gdstash.file.GDReader;
import org.gdstash.file.GDWriter;
import org.gdstash.item.GDAbstractContainer;
import org.gdstash.item.GDItem;
import org.gdstash.util.FileVersionException;
import org.gdstash.util.GDMsgFormatter;

import java.io.IOException;

public class GDCharStashPage extends GDAbstractContainer {
    private static final int BLOCK = 0;
    private GDChar gdc;
    private int stashVersion;

    public GDCharStashPage(final GDChar gdc, final int stashVersion) {
        super(3);
        this.gdc = gdc;
        this.stashVersion = stashVersion;
        this.changed = false;
    }

    public int getHeight() {
        int h = this.getContainerHeight();
        for (final GDItem item : this.items) {
            final int y = item.getY() + item.getImage().getHeight() / 32;
            if (h < y) {
                h = y;
            }
        }
        return h;
    }

    public int getWidth() {
        int w = this.getContainerWidth();
        for (final GDItem item : this.items) {
            final int x = item.getX() + item.getImage().getWidth() / 32;
            if (w < x) {
                w = x;
            }
        }
        return w;
    }

    public void read(final int containerVersion) throws IOException {
        int val = 0;
        final GDReader.Block block = new GDReader.Block();
        if (containerVersion >= 6) {
            val = GDReader.readBlockStart(block);
            if (val != 0) {
                throw new FileVersionException(GDMsgFormatter.getString(GDMsgFormatter.rbMsg, "ERR_UNSUPPORTED_VERSION"));
            }
        }
        this.width = GDReader.readEncInt(true);
        this.height = GDReader.readEncInt(true);
        this.items.clear();
        this.removedItems.clear();
        val = GDReader.readEncInt(true);
        for (int i = 0; i < val; ++i) {
            final GDItem item = new GDItem(this.gdc.getCharName(), this.gdc.isHardcore(), 3);
            item.read(containerVersion);
            if (!item.hasErrors()) {
                this.items.add(item);
            } else {
                this.removedItems.add(item);
            }
        }
        if (containerVersion >= 6) {
            GDReader.readBlockEnd(block);
        }
        this.changed = false;
    }

    public void write(final int containerVersion) throws IOException {
        final GDReader.Block block = new GDReader.Block();
        if (containerVersion >= 6) {
            GDWriter.writeBlockStart(block, 0);
        }
        GDWriter.writeInt(this.width);
        GDWriter.writeInt(this.height);
        final int val = this.items.size() + this.removedItems.size();
        GDWriter.writeInt(val);
        for (final GDItem item : this.items) {
            item.write(containerVersion);
        }
        for (final GDItem item : this.removedItems) {
            item.write(containerVersion);
        }
        if (containerVersion >= 6) {
            GDWriter.writeBlockEnd(block);
        }
        this.changed = false;
    }

    public int getByteSize(final int containerVersion) {
        int size = 0;
        if (containerVersion >= 6) {
            size += 4;
            size += 4;
        }
        size += 4;
        size += 4;
        size += 4;
        for (final GDItem item : this.items) {
            size += item.getByteSize(containerVersion);
        }
        for (final GDItem item : this.removedItems) {
            size += item.getByteSize(containerVersion);
        }
        if (containerVersion >= 6) {
            size += 4;
        }
        return size;
    }
}
